/**
 * 
 */
package com.log.database.query;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.log.database.interfaces.LogDatabaseInf;
import com.log.database.interfaces.LogUserLoginInf;
import com.log.database.util.LogDatabaseHelper;
import com.log.database.util.LogSQLQuery;

/**
 * @author vu.hoang.le
 * 
 */
public class LogUserLoginSql implements LogDatabaseInf, LogUserLoginInf {

	private final String TAG = LogUserLoginSql.class.getSimpleName();

	private final Context context;
	private LogDatabaseHelper logDatabaseHelper;
	private SQLiteDatabase userLoginDb;

	/**
	 * @param context
	 */
	public LogUserLoginSql(Context context) {
		this.context = context;
		logDatabaseHelper = new LogDatabaseHelper(this.context);

		Log.i("vulh_" + TAG, "Constructor: LogUserLoginSql");
	}

	@Override
	public Object open() throws SQLException {
		userLoginDb = logDatabaseHelper.getWritableDatabase();
		return this;
	}

	@Override
	public Object close() throws SQLException {
		logDatabaseHelper.close();
		return this;
	}

	@Override
	public Cursor getUserAccount(String userName, String password)
			throws SQLException {
		Cursor userCursor = userLoginDb.query(LogSQLQuery.TBL_USER_LOGIN_NAME,
				new String[] { LogSQLQuery.COL_USER_LOGIN_USER_NAME,
						LogSQLQuery.COL_USER_LOGIN_USER_PASSWORD },
				LogSQLQuery.COL_USER_LOGIN_USER_NAME + "=? and "
						+ LogSQLQuery.COL_USER_LOGIN_USER_PASSWORD + "=?",
				new String[] { userName, password }, null, null, null, "1");

		if (userCursor != null) {
			userCursor.moveToFirst();
			Log.i("vulh_" + TAG, "Existing an user account! line 63");
		}
		return userCursor;
	}

	@Override
	public long addNewUserAccount(String userName, String password)
			throws SQLException {
		ContentValues newUserValues = new ContentValues();
		newUserValues.put(LogSQLQuery.COL_USER_LOGIN_USER_NAME, userName);
		newUserValues.put(LogSQLQuery.COL_USER_LOGIN_USER_PASSWORD, password);

		return userLoginDb.insert(LogSQLQuery.TBL_USER_LOGIN_NAME, null,
				newUserValues);
	}

	@Override
	public void updateCurrentUserInformation(String userName)
			throws SQLException {
		// TODO Auto-generated method stub

	}

}
